<template>
  <transition name="fade">
    <div ref="confirmModal" v-if="isShow" @click.self="closeModal" class="confirm-modal" :class="[option.showMasker ? 'masker':'',theme]">
      <article class="confirm-content">
        <div v-if="option.title" class="confirm-title" :class="option.titleCenter ? 'confirm-text-center':''">{{option.title}}</div>
        <div v-if="option.message" class="confirm-message" :class="[option.title ? 'has-title-message':'',option.messageCenter ? 'confirm-text-center':'']" v-html="option.message"></div>
        <slot></slot>
        <footer v-if="option.showCancelButton" class="confirm-footer" :class="option.message ? 'has-message':''">
          <div @click="clickCancel" class="btn-cancel">{{option.cancelButtonText || '取消'}}</div>
          <div @click="clickConfirm" class="btn-confirm" :class="option.color?'':'theme-fc'"  :style="option.color?'color:'+option.color:''">{{option.confirmButtonText || '确认'}}</div>
        </footer>
        <footer v-else class="confirm-know-footer" :class="option.message ? 'has-message':''">
          <div @click="clickConfirm" class="btn-know" :class="option.color?'':'theme-bg'"  :style="option.color?'background:'+option.color:''">{{option.confirmButtonText || '我知道了'}}</div>
        </footer>
      </article>
    </div>
  </transition>
</template>
<script>
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false,
    },
    option: {
      type: Object,
      default: () => {
        return {
          title: '',
          titleCenter: true,
          message: '',
          messageCenter: true,
          showMasker: true,
          clickCloseModal: false,
          showCancelButton: true,
          cancelButtonText: '',
          confirmButtonText: '',
          onConfirm: null,
          onCancel: null,
          color:''
        }
      }
    },
  },
  data() {
    return {
      theme: localStorage.getItem("colorName") || 'mb5_default',
      callback: null,
    };
  },
  mounted() {
    setTimeout(() => {
      this.theme = localStorage.getItem("colorName") || 'mb5_default';
    }, 3000);
  },
  methods: {
    clickCancel() {
      this.$emit('onCancel');
      if (this.option.onCancel != null) {
        this.option.onCancel();
      }
      if (typeof this.callback === "function") {
        this.callback('onCancel');
      }
    },
    clickConfirm() {
      this.$emit('onConfirm');
      if (this.option.onConfirm != null) {
        this.option.onConfirm();
      }
      if (typeof this.callback === "function") {
        this.callback('onConfirm');
      }
    },
    closeModal() {
      if (this.option.clickCloseModal) {
        this.clickCancel();
      }
    },
  }
};
</script>
<style scoped>
.fade-enter-active {
  animation: show 0.5s forwards;
}
.fade-leave-active {
  animation: hide 0.5s forwards;
}
.confirm-modal {
  position: fixed;
  z-index: 99998;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  top: 0;
  left: 0;
  opacity: 1;
  -webkit-user-select: none;
  -moz-user-select: none;
  -webkit-user-select: none;
  -o-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -moz-touch-callout: none;
  -ms-touch-callout: none;
  touch-callout: none;
}
@keyframes show {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
@keyframes hide {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}
.masker {
  background-color: rgba(0, 0, 0, 0.5);
}
.confirm-content {
  width: 13.5rem;
  background-color: #fff;
  border-radius: 0.5rem;
  overflow: hidden;
  padding-top: 1rem;
}

.confirm-title {
  color: #333;
  font-weight: bold;
  font-size: 0.7rem;
  padding: 0 1rem;
}
.confirm-text-center {
  text-align: center;
}
.has-title-message {
  margin-top: 0.5rem;
}
.confirm-message {
  color: #333;
  font-size: 0.6rem;
  padding-left: 1rem;
  padding-right: 1rem;
  max-height: 7.5rem;
  overflow-y: auto;
}
.confirm-footer {
  margin-top: 1rem;
  border-top: 1px solid #f6f6f6;
  display: flex;
  justify-content: center;
  align-items: center;
}
.btn-cancel {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  border-right: 1px solid #f6f6f6;
  width: 0;
  height: 1.85rem;
  color: #666;
  font-size: 0.7rem;
}
.btn-confirm {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 0;
  height: 1.85rem;
  font-size: 0.7rem;
}
.confirm-know-footer {
  margin-top: 1rem;
  padding-bottom: 1rem;
  display: flex;
  justify-content: center;
  align-items: center;
}
.btn-know {
  width: 5.75rem;
  height: 1.25rem;
  border-radius: 0.625rem;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  font-size: 0.55rem;
}
.has-message{
  margin-top: 0.8rem;
}
</style>


